package com.example.hotelsystem.service.impl;

import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.hotelsystem.entity.Power;
import com.example.hotelsystem.entity.RolePower;
import com.example.hotelsystem.entity.User;
import com.example.hotelsystem.mapper.RolePowerMapper;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.PowerService;
import com.example.hotelsystem.service.RolePowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2020-10-13
 */
@Service
public class RolePowerServiceImpl extends ServiceImpl<RolePowerMapper, RolePower> implements RolePowerService {

    @Autowired
    RolePowerService RolePowerService;

    @Autowired
    PowerService powerService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result getRolePower(Long roleId){
        QueryWrapper<RolePower> Query = new QueryWrapper<>();
        Query.eq("role_id",roleId);
        List<RolePower> list = RolePowerService.list(Query);
        ArrayList<String> arrayList = new ArrayList();
        HashMap<String,Object> map = new HashMap<>();
        if(list!=null){
            for(RolePower rp :list){
                QueryWrapper<Power> powerQuery = new QueryWrapper<>();
                powerQuery.eq("id",rp.getPowerId());
                Power power = powerService.getOne(powerQuery);
                arrayList.add(power.getPowerName());
            }
            map.put("powerList",arrayList);
            return Result.success("获取成功",map);
        }else{
            return Result.error(CodeError.GET_FAIL,"获取失败");
        }

    }
}
